<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of replyAction
 *
 * @author xlp
 */
class replyAction extends commonAction {

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
        $this->Status = array(1 => '正常', 0 => '<span class="red">受限</span>');
        $this->matchType = array(0 => '全匹配', 1 => '模糊');
        $this->replyType = array(0 => '文本', 1 => '图文');
        $this->newsType = array(1 => '单条图文', 2 => '<span class="red">多条图文</span>');
        $this->assign(array('status' => $this->Status, 'matchType' => $this->matchType, 'newsType' => $this->newsType));
    }

    function index() {
        $p = $this->_getid('p');
        $rs = D('reply')->where(array('aid' => admin::$adminInfo['aid']))->order('id DESC')->page($p)->findAll();
        $this->assign(array('rs' => $rs, 'p' => $p, 'replyType' => $this->replyType, 'total' => D('reply')->getTotal()));
        $this->display();
    }

    function add() {
        $news = M('article')->field('id,title,type')->where(array('cid' => 20, 'status' => 1, 'fid' => 0))->order('id desc')->limit(0, 20)->findAll();
        unset($this->replyType[3]);
        $this->assign(array('news' => $news, 'replyType' => $this->replyType, 'action' => C('reply', 'default_key')));
        $this->display();
    }

    function edit() {
        $id = $this->_getid('id');
        if (!$id) {
            showError('参数丢失');
        }
        $rs = D('reply')->where(array('id' => $id, 'aid' => admin::$adminInfo['aid']))->find();
        if (!$rs) {
            showError('内容不存在');
        }
        unset($this->replyType[3]);
        $news = M('article')->field('id,title,type')->where(array('aid' => admin::$adminInfo['aid'], 'status' => 1, 'fid' => 0))->order('id desc')->limit(0, 20)->findAll();
        $this->assign(array('news' => $news, 'rs' => $rs, 'replyType' => $this->replyType, 'status' => $this->Status, 'action' => C('reply', 'default_key')));
        $this->display();
    }

    function post() {
        $objData = array();
        $fields = array('match_type' => 0, 'keyword' => '', 'reply_type' => '', 'content' => 0, 'fid' => 0, 'status' => 0);
        foreach ($fields as $key => $val) {
            $objData[$key] = $this->_post($key, $val);
        }
        if (!$objData['keyword']) {
            $this->JsonReturn('关键字不能为空');
        }
        if (D('reply')->checkReplyIsExists($objData['keyword'])) {
            $this->JsonReturn('error', 'keyword', 0);
        }
        if ($objData['reply_type'] == 0) {
            $objData['fid'] = 0;
        }
        if ($objData['content']) {
            $objData['content'] = $this->_postContent('content');
        }
        if ($objData['reply_type'] == 2) {
            $objData['fid'] = $this->_postid('action_id');
        }
        $objData['aid'] = admin::$adminInfo['aid'];
        F('replyCache_u', null); //清理自定义回复缓存文件
        if (D('reply')->insert($objData)) {
            $this->JsonReturn('ok', null, 1);
        } else {
            $this->JsonReturn('error');
        }
    }

    function save() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        $objData = array();
        $fields = array('match_type' => 0, 'keyword' => '', 'reply_type' => '', 'content' => '', 'fid' => 0, 'status' => 0);
        foreach ($fields as $key => $val) {
            $objData[$key] = $this->_post($key, $val);
        }
        if (!$objData['keyword']) {
            $this->JsonReturn('error');
        }
        if ($objData['reply_type'] == 0) {
            $objData['fid'] = 0;
        }
        if ($objData['content']) {
            $objData['content'] = $this->_postContent('content');
        }
        if ($objData['reply_type'] == 2) {
            $objData['fid'] = $this->_postid('action_id');
        }
        //修正参数
        $checkKeyWord = D('reply')->checkReplyIsExists($objData['keyword']);
        if ($checkKeyWord && ($checkKeyWord['id'] != $id)) {
            $this->JsonReturn('error', 'keyword', 0);
        }
        F('replyCache_u', null); //清理自定义回复缓存文件
        D('reply')->update($objData, array('id' => $id, 'aid' => admin::$adminInfo['aid']));
        $this->JsonReturn('ok', null, 1);
    }

    function delete() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        $rs = D('reply')->where(array('id' => $id, 'aid' => admin::$adminInfo['aid']))->find();
        if ($rs) {
            if ($rs['fid']) {
                if ($rs['reply_type'] == 1) {//图文
                    M('article')->delete(array('id' => $rs['fid'], 'aid' => admin::$adminInfo['aid']));
                    M('article')->delete(array('fid' => $rs['fid'], 'aid' => admin::$adminInfo['aid']));
                }
            }
        } else {
            $this->JsonReturn('error');
        }
        F('replyCache_u', null); //清理自定义回复缓存文件
        D('reply')->delete(array('id' => $id, 'aid' => admin::$adminInfo['aid']));
        $this->JsonReturn('ok', null, 1);
    }

    /*
     * 关注回复
     */

    function follow() {
        unset($this->replyType[2], $this->replyType[3]);
        $news = M('article')->field('id,title,type')->where(array('cid' => 20, 'status' => 1, 'fid' => 0))->order('id desc')->limit(0, 20)->findAll();
        $this->assign(array('news' => $news, 'rs' => parent::_getAdminSetting('auto_reply'), 'replyType' => $this->replyType, 'action' => C('reply', 'default_key')));
        $this->display();
    }

    /*
     * 保存回复
     */

    function savefollow() {
        $init = parent::_getDefaultSetting('auto_reply');
        foreach ($init as $k => $v) {
            $init[$k] = $this->_post($k, $v);
        }
        if ($init['subscribe']) {
            $init['subscribe'] = $this->_postContent('subscribe');
        }
        if ($init['no_result']) {
            $init['no_result'] = $this->_postContent('no_result');
        }
        if ($init['reply_type'] == 0) {
            $init['fid'] = 0;
        }
        if ($init['reply_type'] == 2) {
            $init['fid'] = $this->_postid('action_id', 0);
        }
        parent::_setAdminSetting('auto_reply', $init);
        $setting = parent::_getAdminSetting();
        foreach ($setting as $key => $val) {
            $setting[$key] = json_decode($val, true);
        }
        //写入缓存
        if (parent::_checkIsAdmin()) {
            F('weixin/setting', $setting);
        }
        F('weixin/setting_aid_' . admin::$adminInfo['aid'], $setting);
        //更新缓存文件
        F('autoReply_u' . admin::$adminInfo['aid'], $setting['auto_reply']);
        $this->JsonReturn('操作成功', null, 1);
    }

}
